package Vaadin.ViewController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

 
 
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.FailedEvent;
import com.vaadin.ui.Upload.FailedListener;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Upload.SucceededListener;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

public class DataFileSelection {
	
	private File dataFile;
	



	private Window window;
	
	Label wekaResult ;
	
	 

	public DataFileSelection(Window _window) {
		window = _window;
		selectionScreen(window); 	
		wekaResult.setValue("");
	}
	
	public File getDataFile() {
		return dataFile;
	}

	private void setDataFile(File dataFile) {
		this.dataFile = dataFile;
	}

	public Window getWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}
	
	private void selectionScreen(Window window){
		window.removeAllComponents();
		
 		// Create the Accordion.
 		Accordion accordion = new Accordion();
 		// Have it take all space available in the layout.
 		accordion.setSizeFull();
		
		//Label uploadDataFilePrompt = new Label("Please upload the data file for data mining.");
 		Upload upload = FileUploadTab();
 		
 		 wekaResult = new Label("Please select the data file URL you want to mine from the repository.");
 		Label dataFileURL = new Label("Please select the data file URL you want to mine from the repository.");
 		Label selectFromRepository = new Label("Please select the data you want to mine from the repository.");
		Label connectDatabase = new Label("Please upload the data file for data mining.");
		
		
//		// Create the Upload component.
//		Upload upload = new Upload("Upload the file here", this);
		
		
		
		
 		// Add the components as tabs in the Accordion.
 		accordion.addTab(upload, "Upload the data file.", null);
 		accordion.addTab(wekaResult, "Weka Data Mining Result.", null);
 		
 		accordion.addTab(dataFileURL, "Upload the data url.", null);
 		accordion.addTab(selectFromRepository, "Select from repository.", null);
 		accordion.addTab(connectDatabase, "Connect to a database.", null);

 		
 		
 		// A container for the Accordion.
 		Panel panel = new Panel("Tasks");
 		panel.setWidth("600px");
 		panel.setHeight("400px");
 		panel.addComponent(accordion);
 		// Trim its layout to allow the Accordion take all space.
 		panel.getLayout().setSizeFull();
 		panel.getLayout().setMargin(false);
		
		
		
		
		window.addComponent(panel);
		
		
	}// end of method SelectionScreen
	
	public void wekaResultUpdate (String _wekaResult){
		
		wekaResult.setValue(_wekaResult) ;
		wekaResult.setContentMode(Label.CONTENT_PREFORMATTED);

	}
	
	
	private Upload FileUploadTab(){
		
		Receiver uploadReceiver = new FileReceiver();
		
		Upload upload = new Upload("Please select the weka file for immediate upload...", uploadReceiver);
		
		UploadSucceedListener uploadSucceedListener = new UploadSucceedListener(); 
		upload.addListener(uploadSucceedListener);
		
		UploadFailedListener uploadFailedListener = new UploadFailedListener();
		upload.addListener(uploadFailedListener);
		
        // Make uploading start immediately when file is selected
        upload.setImmediate(true);
        upload.setButtonCaption("Select file");
		
        return upload;
	} //FileUpload

	
	
	class FileReceiver implements Receiver {

		/**
		 * 
		 */
		private static final long serialVersionUID = 2824068130532949574L;

		// Callback method to begin receiving the upload.
		@Override
		public OutputStream receiveUpload(String filename, String MIMEType) {
			// TODO Auto-generated method stub
			FileOutputStream fos= null; // Output stream to write to
		 
			
			setDataFile( new File("/home/ugur/weka/uploadedfiles/" + filename));
			
			try {
				// Open the file for writing.
				fos = new FileOutputStream(getDataFile());
			} catch (final java.io.FileNotFoundException e) {
				// TODO: handle exception
				// Error while opening the file. Not reported here.
				e.printStackTrace();
				return null;
			}				
			
			return fos; // Return the output stream to write to

		}
		
	} // end of class FileReceiver
	
	
	class UploadFailedListener implements FailedListener{

		/**
		 * 
		 */
		private static final long serialVersionUID = 6647157521401901045L;

		@Override
		public void uploadFailed(FailedEvent event) {
			// Log the failure on screen.
//				window.addComponent(new Label("Uploading "
//		                + event.getFilename() + " of type '"
//		                + event.getMIMEType() + "' failed."));

			
		}
		
	} // end of class UploadFailedListener
	
	
	class UploadSucceedListener implements SucceededListener{

		/**
		 * 
		 */
		private static final long serialVersionUID = 8019126669117803980L;

		@Override
		public void uploadSucceeded(SucceededEvent event) {
			// TODO Auto-generated method stub
			
		       // Log the upload on screen.
//					window.addComponent(new Label("File " + event.getFilename()
//			                + " of type '" + event.getMIMEType()
//			                + "' uploaded."));
			getWindow().showNotification("File Uploaded", getDataFile().getName(), Notification.TYPE_WARNING_MESSAGE);
			System.out.println(getDataFile().getPath() );
			
			String wekaResult = weka.console.CommandPrompt.consoleRun("weka.classifiers.trees.J48", getDataFile().getPath());
			wekaResultUpdate(wekaResult);
			
			
			
 			
			//WekaConsoleOutWindow wekaConsoleOutWindow = new WekaConsoleOutWindow(mainWindow, wekaFile, wekahelloApplication);
			//mainWindow.addWindow(wekaConsoleOutWindow);
			
			// when the file is get through the portal direct it to the weka for processing!
			// the resulting data will also be returned to portal to be shown appropriatlly...
			
			
		} // end of class UploadSucceedListener

	}
	
	
} // end of class

